package com.github.doobo.utils;


import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * 简单获取IP地址
 */
public abstract class IpAddressUtils {

	/**
	 * 获取本地网卡ip
	 */
	public static String getLocalAddress() {
		try {
			// Traversal Network interface to get the first non-loopback and non-private address
			Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
			//ipv4 地址集
			ArrayList<String> ipv4Result = new ArrayList<>();
			//ipv6 地址集
			ArrayList<String> ipv6Result = new ArrayList<>();
			//遍历网卡
			while (enumeration.hasMoreElements()) {
				final NetworkInterface networkInterface = enumeration.nextElement();
				final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
				while (en.hasMoreElements()) {
					final InetAddress address = en.nextElement();
					if (!address.isLoopbackAddress()) {
						if (address instanceof Inet6Address) {
							ipv6Result.add(normalizeHostAddress(address));
						} else {
							ipv4Result.add(normalizeHostAddress(address));
						}
					}
				}
			}
			// prefer ipv4
			if (!ipv4Result.isEmpty()) {
				for (String ip : ipv4Result) {
					if (ip.startsWith("127.0")) {
						continue;
					}
					return ip;
				}
				//存在多个ip的时候，获取最后一个
				return ipv4Result.get(ipv4Result.size() - 1);
			} else if (!ipv6Result.isEmpty()) {
				return ipv6Result.get(0);
			}
			//If failed to find,fall back to localhost
			final InetAddress localHost = InetAddress.getLocalHost();
			return normalizeHostAddress(localHost);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String normalizeHostAddress(final InetAddress localHost) {
		if (localHost instanceof Inet6Address) {
			return "[" + localHost.getHostAddress() + "]";
		} else {
			return localHost.getHostAddress();
		}
	}

	public static void main(String[] args) {
		System.out.println(getLocalAddress());
	}
}
